﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class AppLibraryButtons1 : YJ.Data.Interface.IAppLibraryButtons1
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public AppLibraryButtons1()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibraryButtons1实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.AppLibraryButtons1 model)
        {
            string sql = @"INSERT INTO AppLibraryButtons1
				(ID,AppLibraryID,ButtonID,Name,Events,Ico,Sort,Type,ShowType,IsValidateShow) 
				VALUES(:ID,:AppLibraryID,:ButtonID,:Name,:Events,:Ico,:Sort,:Type,:ShowType,:IsValidateShow)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":AppLibraryID", OracleDbType.Varchar2){ Value = model.AppLibraryID },
				model.ButtonID == null ? new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = model.ButtonID },
				new OracleParameter(":Name", OracleDbType.Varchar2){ Value = model.Name },
				new OracleParameter(":Events", OracleDbType.Varchar2){ Value = model.Events },
				new OracleParameter(":Ico", OracleDbType.Varchar2){ Value = model.Ico },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				new OracleParameter(":Type", OracleDbType.Int32){ Value = model.Type },
				new OracleParameter(":ShowType", OracleDbType.Int32){ Value = model.ShowType },
				new OracleParameter(":IsValidateShow", OracleDbType.Int32){ Value = model.IsValidateShow }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibraryButtons1实体类</param>
        public int Update(YJ.Data.Model.AppLibraryButtons1 model)
        {
            string sql = @"UPDATE AppLibraryButtons1 SET 
				AppLibraryID=:AppLibraryID,ButtonID=:ButtonID,Name=:Name,Events=:Events,Ico=:Ico,Sort=:Sort,Type=:Type,ShowType=:ShowType,IsValidateShow=:IsValidateShow
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":AppLibraryID", OracleDbType.Varchar2){ Value = model.AppLibraryID },
				model.ButtonID == null ? new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = model.ButtonID },
				new OracleParameter(":Name", OracleDbType.Varchar2){ Value = model.Name },
				new OracleParameter(":Events", OracleDbType.Varchar2){ Value = model.Events },
				new OracleParameter(":Ico", OracleDbType.Varchar2){ Value = model.Ico },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				new OracleParameter(":Type", OracleDbType.Int32){ Value = model.Type },
				new OracleParameter(":ShowType", OracleDbType.Int32){ Value = model.ShowType },
				new OracleParameter(":IsValidateShow", OracleDbType.Int32){ Value = model.IsValidateShow },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM AppLibraryButtons1 WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.AppLibraryButtons1> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.AppLibraryButtons1> List = new List<YJ.Data.Model.AppLibraryButtons1>();
            YJ.Data.Model.AppLibraryButtons1 model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.AppLibraryButtons1();
                model.ID = dataReader.GetString(0).ToGuid();
                model.AppLibraryID = dataReader.GetString(1).ToGuid();
                if (!dataReader.IsDBNull(2))
                    model.ButtonID = dataReader.GetString(2).ToGuid();
                model.Name = dataReader.GetString(3);
                model.Events = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Ico = dataReader.GetString(5);
                model.Sort = dataReader.GetInt32(6);
                model.Type = dataReader.GetInt32(7);
                model.ShowType = dataReader.GetInt32(8);
                model.IsValidateShow = dataReader.GetInt32(9);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibraryButtons1> GetAll()
        {
            string sql = "SELECT * FROM AppLibraryButtons1";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.AppLibraryButtons1> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM AppLibraryButtons1";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.AppLibraryButtons1 Get(Guid id)
        {
            string sql = "SELECT * FROM AppLibraryButtons1 WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibraryButtons1> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByAppID(Guid id)
        {
            string sql = "DELETE FROM AppLibraryButtons1 WHERE AppLibraryID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }


        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibraryButtons1> GetAllByAppID(Guid id)
        {
            string sql = "SELECT * FROM AppLibraryButtons1 WHERE AppLibraryID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibraryButtons1> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}